package com.guofen.controller.sjq;

import com.guofen.pojo.SjqCompostProduceElectricity;
import com.guofen.pojo.SjqCompostProduceFault;
import com.guofen.service.sjq.SjqCompostProduceFaultService;
import com.guofen.service.sjq.SjqCompostProduceInspectionService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/sjq/compostProduceFault")
@Controller
@Api(tags = "SjqCompostProduceFaultController", description = "堆肥生产现场故障记录")
public class SjqCompostProduceFaultController {
    @Autowired
    private SjqCompostProduceFaultService sjqCompostProduceFaultService;
    @Autowired
    private SjqCompostProduceInspectionService sjqCompostProduceInspectionService;

    @ApiOperation("添加记录_")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name="inspectionId",value="记录ID",required=true) @RequestParam String inspectionId,
                      @ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                       @ApiParam(name="faultTime",value="故障停机时间",required=true) @RequestParam String faultTime,
                       @ApiParam(name="operator",value="操作人",required=true) @RequestParam String operator,
                       @ApiParam(name="faultContent",value="故障情况描述",required=true) @RequestParam String faultContent,
                       @ApiParam(name="serviceTimeStart",value="维修开始时间",required=true) @RequestParam String serviceTimeStart,
                       @ApiParam(name="serviceTimeEnd",value="维修结束时间",required=true) @RequestParam String serviceTimeEnd,
                       @ApiParam(name="servicemanInCharge",value="维修负责人",required=true) @RequestParam String servicemanInCharge,
                       @ApiParam(name="serviceman",value="维修人员名字",required=true) @RequestParam String serviceman,
                       @ApiParam(name="faultReason",value="故障原因分析",required=true) @RequestParam String faultReason,
                       @ApiParam(name="serviceMethod",value="维修方法",required=true) @RequestParam String serviceMethod,
                       @ApiParam(name="proposer",value="维修方法提出人",required=true) @RequestParam String proposer,
                       @ApiParam(name="recoverTime",value="故障回复时间·",required=true) @RequestParam String recoverTime,
                       @ApiParam(name="inspector",value="检查人",required=true) @RequestParam String inspector){

        if (sjqCompostProduceFaultService.add(inspectionId,equipmentName,faultTime,operator,
                faultContent,serviceTimeStart,serviceTimeEnd,servicemanInCharge,serviceman,
                faultReason,serviceMethod,proposer,recoverTime,inspector))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("填写故障记录 前半")
    @RequestMapping(value = "/addFrontPart", method = RequestMethod.POST)
    @ResponseBody
    public Result addFrontPart(@ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName,
                               @ApiParam(name="faultTime",value="故障停机时间",required=true) @RequestParam String faultTime,
                               @ApiParam(name="operator",value="操作人",required=true) @RequestParam String operator,
                               @ApiParam(name="faultContent",value="故障情况描述",required=true) @RequestParam String faultContent) {
        return ResultGenerator.genSuccessResult(sjqCompostProduceFaultService.addFrontPart(equipmentName,faultTime,operator,faultContent));
    }

    @ApiOperation("填写故障记录 后半")
    @RequestMapping(value = "/addAfterPart", method = RequestMethod.POST)
    @ResponseBody
    public Result addAfterPart(@ApiParam(name="faultId",value="故障记录ID",required=true) @RequestParam Integer faultId,
                               @ApiParam(name="serviceTimeStart",value="维修开始时间",required=true) @RequestParam String serviceTimeStart,
                               @ApiParam(name="serviceTimeEnd",value="维修结束时间",required=true) @RequestParam String serviceTimeEnd,
                               @ApiParam(name="servicemanInCharge",value="维修负责人",required=true) @RequestParam String servicemanInCharge,
                               @ApiParam(name="serviceman",value="维修人员名字",required=true) @RequestParam String serviceman,
                               @ApiParam(name="faultReason",value="故障原因分析",required=true) @RequestParam String faultReason,
                               @ApiParam(name="serviceMethod",value="维修方法",required=true) @RequestParam String serviceMethod,
                               @ApiParam(name="proposer",value="维修方法提出人",required=true) @RequestParam String proposer,
                               @ApiParam(name="recoverTime",value="故障回复时间·",required=true) @RequestParam String recoverTime,
                               @ApiParam(name="inspector",value="检查人",required=true) @RequestParam String inspector) {
        return ResultGenerator.genSuccessResult(sjqCompostProduceFaultService.addAfterPart(faultId,serviceTimeStart,serviceTimeEnd,
                servicemanInCharge,serviceman,faultReason,serviceMethod,proposer,recoverTime,inspector));
    }

    @ApiOperation("获取 当日 最新 一条记录")
    @RequestMapping(value = "/getlast_day",method = RequestMethod.GET)
    @ResponseBody
    public Result getlast_day(){
        return  ResultGenerator.genSuccessResult(sjqCompostProduceFaultService.getlast_day());
    }

    @ApiOperation("获取 本周 有错误的 未填写故障记录 的 记录")
    @RequestMapping(value = "/getlast_fault",method = RequestMethod.GET)
    @ResponseBody
    public Result getlastWithFault(){
        return  ResultGenerator.genSuccessResult(sjqCompostProduceInspectionService.getlastWithFault());
    }

    @ApiOperation("根据 设备名称 获取故障记录")
    @RequestMapping(value = "/getLastByEquipmentName", method = RequestMethod.POST)
    @ResponseBody
    public Result getLastByEquipmentName(
            @ApiParam(name="equipmentName",value="设备名称",required=true) @RequestParam String equipmentName) {
        return ResultGenerator.genSuccessResult(sjqCompostProduceFaultService.getLastByEquipmentName(equipmentName));
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(sjqCompostProduceFaultService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="id",required=true) @RequestParam(required = true) Integer id,
                         @ApiParam(name="date",value="date",required=false) @RequestParam(required = false) String date,
                         @ApiParam(name="equipment_name",value="equipment_name",required=false) @RequestParam(required = false) String equipment_name,
                         @ApiParam(name="fault_time",value="fault_time",required=false) @RequestParam(required = false) String fault_time,
                         @ApiParam(name="operator",value="operator",required=false) @RequestParam(required = false) String operator,
                         @ApiParam(name="fault_content",value="fault_content",required=false) @RequestParam(required = false) String fault_content,
                         @ApiParam(name="service_time_start",value="service_time_start",required=false) @RequestParam(required = false) String service_time_start,
                         @ApiParam(name="service_time_end",value="service_time_end",required=false) @RequestParam(required = false) String service_time_end,
                         @ApiParam(name="serviceman_in_charge",value="serviceman_in_charge",required=false) @RequestParam(required = false) String serviceman_in_charge,
                         @ApiParam(name="serviceman",value="serviceman",required=false) @RequestParam(required = false) String serviceman,
                         @ApiParam(name="fault_reason",value="fault_reason",required=false) @RequestParam(required = false) String fault_reason,
                         @ApiParam(name="service_method",value="service_method",required=false) @RequestParam(required = false) String service_method,
                         @ApiParam(name="proposer",value="proposer",required=false) @RequestParam(required = false) String proposer,
                         @ApiParam(name="recover_time",value="recover_time",required=false) @RequestParam(required = false) String recover_time,
                         @ApiParam(name="inspector",value="inspector",required=false) @RequestParam(required = false) String inspector
    ) {
        SjqCompostProduceFault record = new SjqCompostProduceFault();

        record.setId(id);
        record.setDate(date);
        record.setEquipmentName(equipment_name);
        record.setFaultTime(fault_time);
        record.setOperator(operator);
        record.setFaultContent(fault_content);
        record.setServiceTimeStart(service_time_start);
        record.setServiceTimeEnd(service_time_end);
        record.setServicemanInCharge(serviceman_in_charge);
        record.setServiceman(serviceman);
        record.setFaultReason(fault_reason);
        record.setServiceMethod(service_method);
        record.setProposer(proposer);
        record.setRecoverTime(recover_time);
        record.setInspector(inspector);

        return ResultGenerator.genSuccessResult(sjqCompostProduceFaultService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间(时间戳：毫秒)",required = false) @RequestParam(required = false) Long startTime,
                         @ApiParam(name="endTime",value="截止时间(时间戳：毫秒)", required = false) @RequestParam(required = false) Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(sjqCompostProduceFaultService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(sjqCompostProduceFaultService.getByTime(pageSize, currentPage, startTime, endTime));
    }

}
